package com.example.saas.context;

import cn.hutool.core.util.ObjUtil;

/**
 * 租户上下文信息
 */
public class TenantContext {

    /**
     * 设置租户Id
     */
    private static final ThreadLocal<String> TENANT_HOLDER = new ThreadLocal<>();

    /**
     * 设置是否过滤租户标识
     */
    private static final ThreadLocal<Boolean> TENANT_SKIP_FLAG = new ThreadLocal<>();

    public static void setTenantId(String tenantId){
        TENANT_HOLDER.set(tenantId);
    }

    public static String getTenantId(){
        return TENANT_HOLDER.get();
    }

    public static void setTenantSkipFlag(Boolean flag){
        TENANT_SKIP_FLAG.set(flag);
    }

    public static Boolean getTenantSkipFlag(){
        return ObjUtil.isNotNull(TENANT_SKIP_FLAG.get()) && TENANT_SKIP_FLAG.get();
    }

    public static void clear(){
        TENANT_HOLDER.remove();
        TENANT_SKIP_FLAG.remove();
    }
}
